package com.toocms.jinjuhang.ui.mine.national_agency

import android.graphics.BitmapFactory
import android.os.Bundle
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.OrganizationMap
import com.toocms.jinjuhang.databinding.FgtNationalAgencyBinding
import com.toocms.jinjuhang.ui.base.BaseFgt
import com.toocms.tab.map.utils.LocationUtils

class NationalAgencyFgt() : BaseFgt<FgtNationalAgencyBinding, NationalAgencyModel>() {

    private var isFirstLocation = true // 是否为第一次定位，用于区分是否显示地图移动动画

    override fun onFragmentCreated() {
        topBar.setTitle(R.string.str_national_distribution_institutions)
        binding.contentMv.run {
            onCreate(arguments)
            map
        }.run {
            val markers = ArrayList<MarkerOptions>()
            OrganizationMap.organization.forEach { item ->
                if (item.latitude.isNullOrEmpty() || item.longitude.isNullOrEmpty()) return@forEach
                val markerOption = MarkerOptions().apply {
                    position(LatLng(item.latitude.toDouble(), item.longitude.toDouble()))
                    draggable(false)
                    anchor(0.5f, 1f)
                    val iconBitmap =
                        BitmapFactory.decodeResource(resources, R.mipmap.icon_location0)
                    val iconBitmapDescriptor = BitmapDescriptorFactory.fromBitmap(iconBitmap)
                    icon(iconBitmapDescriptor)
                }
                markers += markerOption
            }
            if (markers.isNotEmpty()) {
                addMarkers(markers, true)
            }
            setOnMapLoadedListener {
                if (!LocationUtils.isLocationEnabled()) LocationUtils.toOpenGPS(this@NationalAgencyFgt)
                else viewModel.requestPermissions()
            }
            uiSettings
        }.apply {
            isZoomControlsEnabled = false
        }
    }

    override fun onResume() {
        super.onResume()
        binding.contentMv.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding.contentMv.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.contentMv.onDestroy()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.contentMv.onSaveInstanceState(outState)
    }

    override fun getLayoutResId() = R.layout.fgt_national_agency

    override fun getVariableId() = BR.nationalAgencyModel

    override fun viewObserver() {
        viewModel.locationChanged.observe(this) { latLng ->
            if (isFirstLocation) {
                binding.contentMv.map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18F))
                isFirstLocation = false
            } else {
                binding.contentMv.map.animateCamera(
                    CameraUpdateFactory.newLatLngZoom(latLng, 18F),
                    500,
                    null
                )
            }
        }
    }
}